﻿using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WeChat.Common;
using WxPayAPI;
using System.Configuration;
namespace WeChat.MenuReservation
{
    public partial class ReserveMenu : System.Web.UI.Page
    {
        public static string wxEditAddrParam { get; set; }
        public string openid;
        public string wxopenid;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string wechatstate = ConfigurationManager.AppSettings["wechatstate"];
                if (wechatstate == "1")
                {
                    string userAgent = Request.UserAgent;
                    if (userAgent.ToLower().Contains("micromessenger"))
                    {
                        JsApiPay jsApiPay = new JsApiPay(this);
                        try
                        {
                            //调用【网页授权获取用户信息】接口获取用户的openid和access_token
                            jsApiPay.GetOpenidAndAccessToken();

                            //获取收货地址js函数入口参数
                            wxEditAddrParam = jsApiPay.GetEditAddressParameters();
                            ApplicationLog.Write("微信充值openid:" + jsApiPay.openid);
                            wxopenid = jsApiPay.openid;
                        }
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
                        catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
                        {
                            Response.Write("<span style='color:#FF0000;font-size:20px'>" + "submitorder页面加载出错，请重试" + "</span>");
                        }
                    }
                }



                //认证后得到code值
                if (Request.QueryString["code"] != null || Request.QueryString["code"] != "")
                {
                    string code = Request.QueryString["code"];
                    ApplicationLog.Write("code:" + code);
                    if (Session["openid"] == null || Session["openid"].ToString() == "")
                    {
                        Session["openid"] = OAuthHelper.getOpenid(code);
                    }
                   // openid = "opc0cwB3k-hFwKzwRZ68dp-qIrqs";
                   openid = Session["openid"].ToString();
                  
                    ApplicationLog.Write("获取到的openid:" + openid);
                    // 验证是否绑定账号,未绑定则跳转提示绑定账号
                    if (!new BindIdBLL().verifyId(openid))
                    {
                        Response.Redirect("/Binding.aspx?openid=" + openid);
                    }
                }
            }
        }
    }
}